package com.itany.mall.run;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

import com.itany.mall.entity.User;
import com.itany.mall.jdbc.exception.DataAccessException;
import com.itany.mall.jdbc.transaction.TransactionManager;
import com.itany.mall.jdbc.transaction.impl.TransactionManagerImpl;
import com.itany.mall.jdbc.util.JDBCUtil;

public class Test {
	public static void main(String[] args) {

		String sql = " select id, name, password, role from t_user where name = 'aaaaa' ";
		
		try {
			Connection con = JDBCUtil.getConnection();
			PreparedStatement ps = con.prepareStatement(sql);
			ResultSet res = ps.executeQuery();
			res.next();
			System.out.println(res.getString("id"));
			System.out.println(res.getString("name"));
			System.out.println(res.getString("password"));
			System.out.println(res.getInt("role"));
			System.out.println(res.getString("role"));
			System.out.println(res.getObject("role"));
			JDBCUtil.close(res, ps, con);
		} catch (Exception e) {
			e.printStackTrace();
		}
		

	}

	public static void action() {
		Runnable run = new Runnable() {
			@Override
			public void run() {
				System.out.println(Thread.currentThread().getName() + " 请输入id");
				long id = new Scanner(System.in).nextLong();

				System.out.println(Thread.currentThread().getName() + " 请输入name");
				String name = new Scanner(System.in).nextLine();

				serviceProxy(id, name);
				System.out.println("完成");
			}
		};

		new Thread(run).start();
		new Thread(run).start();
	}

	public static void serviceProxy(long id, String name) {
		TransactionManager tm = new TransactionManagerImpl();
		// 事务的管理跟service有关
		Connection con = null;
		try {
			tm.beginTransaction();

			serviceSome(id, name);

			tm.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tm.rollback();
		}
	}

	public static void serviceSome(long id, String name) {
		User u = daoSelect(id);
		if (u == null) {
			System.out.println("service 异常");
			return;
		}
		u.setName(name);
		daoUpdate(u);
	}

	public static User daoSelect(long userId) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		try {
			String sql = " select id, name, password, phone, email, role from t_user where id = ? ";
			con = JDBCUtil.getConnection();
			System.out.println(
					Thread.currentThread().getName() + " dao层select方法的con: " + con.hashCode());
			ps = con.prepareStatement(sql);
			ps.setObject(1, userId);
			res = ps.executeQuery();
			if (res.next()) {
				User u = new User();
				u.setId(res.getLong("id"));
				u.setName(res.getString("name"));
				u.setPassword(res.getString("password"));
				u.setPhone(res.getString("phone"));
				u.setEmail(res.getString("email"));
				u.setRole(res.getInt("role"));
				return u;
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			throw new DataAccessException();
		} finally {
			JDBCUtil.close(res, ps, null);
		}
	}

	public static void daoUpdate(User u) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			String sql = " update t_user set name = ?, password = ?, phone = ?, email = ?, role = ? where id = ? ";
			con = JDBCUtil.getConnection();
			System.out.println(
					Thread.currentThread().getName() + " dao层update方法的con: " + con.hashCode());
			ps = con.prepareStatement(sql);
			ps.setObject(1, u.getName());
			ps.setObject(2, u.getPassword());
			ps.setObject(3, u.getPhone());
			ps.setObject(4, u.getEmail());
			ps.setObject(5, u.getRole());
			ps.setObject(6, u.getId());
			ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(null, ps, null);
		}
	}

}
